using Godot;
using System;
public class UIElement : Control
{
    private bool _preSelected;
    private Color _preselectColor;
    private Vector2 _start;
    private Vector2 _last;
    public override void _Ready()
    {
        if (HasColor("Preselect", "Selecter"))
            _preselectColor = GetColor("Preselect", "Selecter");
        else
            _preselectColor = Color.FromHsv(1, 1, 1, 0.35f);
    }

    public override void _Draw()
    {
        if (_preSelected)
        {
            DrawRect(new Rect2(0, 0, RectSize.x, RectSize.y), _preselectColor, false);
        }
    }

    public void Reset()
    {
        _preSelected = false;
        Update();
    }
    internal bool OnHandleSelectRect(Rect2 rect)
    {

        var preSelected = rect.Intersects(new Rect2(0, 0, RectSize.x, RectSize.y));
        if (preSelected != _preSelected)
        {
            _preSelected = preSelected;
            Update();
        }
        return preSelected;
    }

    internal void SetMove(Vector2 position)
    {
        position.x = (float)Math.Ceiling(position.x);
        position.y = (float)Math.Ceiling(position.y);
        var offset = position - _start;
        position = _last + offset;
        RectGlobalPosition = position;
        GD.Print(RectGlobalPosition);
        //RectPosition = _last + offset;
    }
    internal void BeginMove(Vector2 position)
    {
        position.x = (float)Math.Ceiling(position.x);
        position.y = (float)Math.Ceiling(position.y);
        _last = RectGlobalPosition;
        _start = position;
    }
    internal void EndMove(Vector2 position)
    {
        _start = Vector2.Zero;
    }
}
